package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.consts.EnvEnum;
import com.harmontronics.hanma.paas.paas.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmReleaseSetupAppService;
import com.harmontronics.hanma.paas.paas.dto.releasesetup.output.HmReleaseSetupResult;
import com.harmontronics.hanma.paas.paas.dto.releasesetup.input.HmReleaseSetupUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.entity.HmReleaseSetup;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 应用发布配置表
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-12 15:39:38
 */
@RestController
@RequestMapping("/api/hmreleasesetup")
@Api(tags = {"应用发布配置表"})
public class HmReleaseSetupController {
    @Autowired
    private HmReleaseSetupAppService hmReleaseSetupappService;



    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmReleaseSetupResult> info(
            @RequestParam EnvEnum setupType) {
        String appUnique = RequestUtils.getAppUnique();
        HmReleaseSetup hmReleaseSetup =hmReleaseSetupappService.findById(appUnique.split("_")[1],setupType);
            HmReleaseSetupResult result = BeanUtil.toBean(hmReleaseSetup,HmReleaseSetupResult.class);
        return ResponseEntity.ok(result);
    }


    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmReleaseSetupUpdateInput hmReleaseSetupInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        String appUnique = RequestUtils.getAppUnique();
        HmReleaseSetup hmReleaseSetup =BeanUtil.toBean(hmReleaseSetupInput,HmReleaseSetup. class);
            hmReleaseSetup.setAppUniqueId(appUnique.split("_")[1]);
            hmReleaseSetupappService.update(hmReleaseSetup);
        return ResponseEntity.ok();
    }

}
